-- global_damage
-- create by wangy
-- 技能：全体伤害。对地牢中所有怪物造成伤害（分已显示和所有怪物两种）
-- 目前支持如下配置：
-- 1、base_global_damage(1200)：对目标造成1200威力基础攻击值的伤害

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 伤害，基础攻击的千分比，保个底
        local force = para[1];
        local baseAttack = InterfaceM.calcBaseAttrib(source, "attack");
        local damage = math.modf(force * baseAttack / 1000);
        damage = math.max(1, damage);

        local addon = 0;
        local args  = {["value"] = damage, ["class_id"] = extra.propertyId,};

        -- 概率造成额外的百分比伤害
        local props = PropM.fetchProps(source, "prob_per_damage");
        for _, prop in ipairs(props) do
            addon = addon + PropM.trigger(source, prop[1], prop[2], args);
        end

        damage = damage + addon;

        -- 修正伤害值（需求是道具伤害，是否被免疫）
        damage = PropertyM.fixPropertyDamage(source, target, 7311, damage);

        -- 遍历所有的怪
        local posList = {};
        local num = 0;
        local totalDamage = 0;
        for p = 1, GRID_SIZE do
            local grid = DungeonM.getGridByPos(p);
            local monster = grid.monster;

            repeat

                -- 是未死亡的怪物
                if not grid:isOpened() or
                    not monster or
                    grid.monster:isDead() then
                    break;
                end

                table.insert(posList, grid:getPos());
                num = num + 1;

                -- 需要UI延迟
                extra["delay_time"] = 0.23 * num;

                -- 增加命中动作
                if SkillM.getSequence(monster) == nil then
                    SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
                end

                SkillM.getSequence(monster):hit(source, monster, skillId);

                -- 受创
                CombatM.receiveDamage(source, monster, damage, skillId, extra);
                totalDamage = totalDamage + damage;

            until true;
        end

        -- 抛出技能攻击事件
        if type(para[2]) == "string" then
            -- 播放特效所需的参数
            local effectInfo = {
                ["move_type"] = para[2],
                ["effect_id"] = para[3],
                ["tail_id"] = para[4] ~= 0 and para[4] or nil,
                ["offset"] = {x = para[5], y = para[6]},
                ["pos"] = posList;
                ["icon_id"] = 7311;
            };

            -- 抛出投掷类攻击事件
            EventMgr.fire(event.SKILL_ATTACK, { ["effect_info"] = effectInfo, });
        end

        -- 如果是玩家，抛出攻击结果事件
        if source.type == OBJECT_TYPE_USER then
            EventMgr.fire(event.HERO_ATTACK_RESULT, { ["damage"] = totalDamage, ["extra"] = extra});
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), arr[2], tonumber(arr[3]), tonumber(arr[4]), tonumber(arr[5]), tonumber(arr[6])};
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        return desc;
    end,
};
